package indi.caibo.service_edu.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import indi.caibo.commonutils.R;
import indi.caibo.service_edu.entity.EduTeacher;
import indi.caibo.service_edu.entity.vo.teacher.TeacherQuery;
import indi.caibo.service_edu.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 讲师 前端控制器
 *
 * @author Bo Cai
 * @since 2021-10-02
 */
@Api(tags = "讲师管理模块")
@RestController
@RequestMapping("/edu-service/teacher")
public class EduTeacherController {
    @Autowired
    private EduTeacherService eduTeacherService;

    @GetMapping("")
    @ApiOperation("获取所有讲师列表")
    public R getTeacherList() {
        return R.ok().data("items", eduTeacherService.list(null));
    }

    @DeleteMapping("delete-by-id/{id}")
    @ApiOperation("根据id删除讲师")
    public R removeTeacher(@ApiParam(name = "id", value = "讲师id", required = true) @PathVariable("id") String id) {
        boolean result = eduTeacherService.removeById(id);
        return result ? R.ok() : R.error().message("删除失败");
    }

    @ApiOperation("分页多条件讲师列表")
    @PostMapping("page-query/{current-page}/{limit}")
    public R pageQuery(
            @ApiParam(name = "current-page", value = "当前页码", required = true)
            @PathVariable("current-page") Long currentPage,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable("limit") Long limit,
            @ApiParam(name = "teacherQuery", value = "查询对象", required = false)
            @RequestBody TeacherQuery teacherQuery) {
        Page<EduTeacher> pageParam = new Page<>(currentPage, limit);
        eduTeacherService.pageQuery(pageParam, teacherQuery);
        List<EduTeacher> records = pageParam.getRecords();
        long total = pageParam.getTotal();
        return R.ok().data("total", total).data("rows", records);
    }

    @ApiOperation("新增讲师")
    @PostMapping("save")
    public R save(
            @ApiParam(name = "teacher", value = "讲师对象", required = true)
            @RequestBody EduTeacher teacher
    ) {
        return eduTeacherService.save(teacher) ? R.ok() : R.error();
    }

    @ApiOperation("根据ID查询讲师")
    @GetMapping("{id}")
    public R getById(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable("id") String id) {
        return R.ok().data("item", eduTeacherService.getById(id));
    }

    @ApiOperation("根据ID修改讲师")
    @PutMapping("{id}")
    public R updateById(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable("id") String id,

            @ApiParam(name = "teacher", value = "讲师对象", required = true)
            @RequestBody EduTeacher teacher) {
        teacher.setId(id);
        return eduTeacherService.updateById(teacher) ? R.ok() : R.error();
    }
}

